package com.karumi.dexter.uitool;

import com.karumi.dexter.ResourceTable;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;
import ohos.global.configuration.Configuration;
/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * 弹框列表适配器
 */
public class ListContainerAdapter extends BaseItemProvider {
    private static final String TAG = ListContainerAdapter.class.getSimpleName();
    private DexterCustomDialog mDialog;
    private InternalListCallback mCallback;
    private Context mContext;

    ListContainerAdapter(DexterCustomDialog dialog, Context context) {
        DexterLogUtil.info(TAG, "Entering DefaultRvAdapter");
        mDialog = dialog;
        mContext = context;
    }

    /**
     * 列表点击回调
     * @param callback callback
     */
    void setCallback(InternalListCallback callback) {
        DexterLogUtil.info(TAG, "Entering setCallback");
        mCallback = callback;
    }

    @Override
    public int getCount() {
        DexterLogUtil.info(TAG, "Entering getCount");
        return mDialog.builder.items.size();
    }

    @Override
    public Object getItem(int pos) {
        DexterLogUtil.info(TAG, "Entering getItem");
        return mDialog.builder.items.get(pos);
    }

    @Override
    public long getItemId(int pos) {
        return 0;
    }

    @Override
    public Component getComponent(int index, Component component, ComponentContainer componentContainer) {
        DexterLogUtil.info(TAG, "Entering getComponent");
        if (component == null) {
            component =
                    LayoutScatter.getInstance(mContext)
                            .parse(ResourceTable.Layout_uilayouts_stack_layout_item, componentContainer, true);
        }
        onPrepareView(index, component);
        return component;
    }

    private void onPrepareView(int index, Component component) {
        DexterLogUtil.info(TAG, "Entering onPrepareView");
        Text title = (Text) component.findComponentById(ResourceTable.Id_title);
        title.setText((String) getItem(index));
        title.setFont(mDialog.builder.regularFont);

        setupGravity((ComponentContainer) component);

        ComponentContainer group = (ComponentContainer) component;
        if (group.getChildCount() == 2) {
            // Remove circular selector from check boxes and radio buttons on Lollipop
            if (group.getComponentAt(0) instanceof Button) {
                group.getComponentAt(0).setBackground(null);
            } else if (group.getComponentAt(1) instanceof Button) {
                group.getComponentAt(1).setBackground(null);
            }
        }
        component.setClickedListener(
                view -> {
                    onItemClicked(view, index);
                });
    }

    private void setupGravity(ComponentContainer view) {
        DexterLogUtil.info(TAG, "Entering setupGravity");
        final DirectionalLayout itemRoot = (DirectionalLayout) view;
        itemRoot.setAlignment(TextAlignment.START);

        if (view.getChildCount() == 2) {
            if (isRTL() && view.getComponentAt(1) instanceof Button) {
                Button first = (Button) view.getComponentAt(1);
                view.removeComponent(first);

                Text second = (Text) view.getComponentAt(0);
                view.removeComponent(second);
                second.setPadding(
                        second.getPaddingRight(),
                        second.getPaddingTop(),
                        second.getPaddingRight(),
                        second.getPaddingBottom());

                view.addComponent(first);
                view.addComponent(second);
            }
        }
    }

    private boolean isRTL() {
        DexterLogUtil.info(TAG, "Entering isRTL");
        Configuration config = mDialog.builder.getContext().getResourceManager().getConfiguration();
        return config.isLayoutRTL;
    }

    private void onItemClicked(Component component, int index) {
        DexterLogUtil.info(TAG, "Entering onClick");
        if (mCallback != null) {
            CharSequence text = null;
            if (mDialog.builder.items != null) {
                text = mDialog.builder.items.get(index);
            }
            mCallback.onItemSelected(mDialog, component, index, text, false);
        }
    }

    /**
     * 弹框点击监听
     */
    interface InternalListCallback {
        /**
         * 点击回应
         * @param dialog dialog
         * @param itemView itemView
         * @param position position
         * @param text text
         * @param longPress longPress
         * @return boolean
         */
        boolean onItemSelected(
                DexterCustomDialog dialog, Component itemView, int position, CharSequence text, boolean longPress);
    }
}
